Imports CapaDeNegocios
Partial Public Class FrmListadoOrdenDePago
    Inherits System.Web.UI.Page
    Public OrdenDePago As CapaDeNegocios.nOrdendePago = New CapaDeNegocios.nOrdendePago
    Public Proveedor As CapaDeNegocios.nProveedor = New CapaDeNegocios.nProveedor
    Public Factura As CapaDeNegocios.nFacturaCompra = New CapaDeNegocios.nFacturaCompra
    Public DetalleOrdenDePago As CapaDeNegocios.nDetalleOrdenDePago = New CapaDeNegocios.nDetalleOrdenDePago

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If
        If Not Page.IsPostBack Then
            LlenarCboProveedor()
        End If
    End Sub
    Sub LlenarCboProveedor()
        CboProveedor.Items.Clear()
        CboProveedor.Items.Add(New ListItem("Ninguno...", -1))
        CboProveedor.DataSource = Proveedor.Listar("", "", "", "", "", "", "", "")
        CboProveedor.DataTextField = "RazonSocial"
        CboProveedor.DataValueField = "CodigoProv"
        CboProveedor.DataBind()
        If CboProveedor.Items.Count > 1 Then
            CboProveedor.SelectedIndex = 1
        End If
        LLenarCboOrden()
        BtnImprimir.Visible = False

        BtnImprimir.OnClientClick = "javascript:var popup = window.open('../Reportes/RptListadoDeOrdenesDePago.aspx',(new Date().getTime()),'Width=900,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
    End Sub
    Sub LLenarCboOrden()
        CboOrden.Items.Clear()
        CboOrden.Items.Add(New ListItem("Ninguna...", -1))
        If CboProveedor.SelectedValue <> -1 Then
            CboOrden.DataSource = OrdenDePago.BuscarFiltrado("", "", "", CboProveedor.SelectedValue.ToString(), "")
            CboOrden.DataTextField = "Numero"
            CboOrden.DataValueField = "CodigoOrdenDePago"
            CboOrden.DataBind()
            If CboOrden.Items.Count > 1 Then
                CboOrden.SelectedIndex = 1
            End If
        End If
    End Sub
    Protected Sub CboProveedor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboProveedor.SelectedIndexChanged
        LLenarCboOrden()
    End Sub

    Protected Sub LnkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkBuscar.Click
        Dim Proveedor As String
        Dim Orden As String
        Proveedor = ""
        Orden = ""
        If CboProveedor.SelectedValue <> -1 Then
            Proveedor = CboProveedor.SelectedValue.ToString()
        End If
        If CboOrden.SelectedValue <> -1 Then
            Orden = CboOrden.SelectedValue.ToString()
        End If
        GridOrdenes.DataSource = OrdenDePago.BuscarFiltrado(Orden, "", "", Proveedor, "")
        GridOrdenes.DataBind()
        BtnImprimir.Visible = True
        Session("RptListadoDeOrdenesDePago") = GridOrdenes.DataSource

    End Sub

    Protected Sub GridOrdenes_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridOrdenes.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim a As LinkButton = e.Row.FindControl("LnkSeleccion")
            Dim b As HiddenField = e.Row.FindControl("HFOrden")
            a.OnClientClick = "javascript:var popup = window.open('FrmListarDetalleOrdenDePago.aspx?IDOrden=" + b.Value.ToString() + "',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
        End If

    End Sub

    Protected Sub GridOrdenes_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridOrdenes.RowCommand
        If e.CommandName = "Eliminar" Then
            Dim IdOrden As String
            Dim IdFactura As String
            IdOrden = e.CommandSource.Parent.Parent.FindControl("HFOrden").Value.ToString()
            Dim dts As New DataSet
            dts = DetalleOrdenDePago.BuscarFiltrado("", "", "", IdOrden)
            Dim Cont As Integer
            Cont = 0
            While Cont < dts.Tables(0).Rows.Count
                IdFactura = Factura.ListarFacturas(0, CInt(e.CommandSource.Parent.Parent.FindControl("HFProveedor").Value.ToString()), dts.Tables(0).Rows(Cont)("Factura").ToString(), "CANCELADA", 0, 0, "", "").Tables(0).Rows(0)("CodigoFactura").ToString()
                Factura.Modificar(IdFactura, "", "", "PENDIENTE", "", "", "", "")
                Cont = Cont + 1
            End While
            DetalleOrdenDePago.Eliminar("", IdOrden)
            OrdenDePago.Eliminar(IdOrden)
            LnkBuscar_Click(sender, e)
            LLenarCboOrden()
        End If
    End Sub

    Protected Sub GridOrdenes_PageIndexChanging(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridOrdenes.PageIndexChanging
        GridOrdenes.PageIndex = e.NewPageIndex
        LnkBuscar_Click(sender, e)
    End Sub

End Class